import { Component, OnInit, Input } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { environment } from '@env/environment';
import { DatePipe } from '@angular/common';
import { StatusFollowService } from '@core/statusFollow.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ExportFileService } from '@core/exportFile';

@Component({
    selector: 'app-zdrwgzqd-wcqk',
    templateUrl: './zdrwgzqd_wcqk.component.html',
    styleUrls: ['./zdrwgzqd_wcqk.component.less']
})
export class ZdrwgzqdWcqkComponent implements OnInit {
    queryParam: any = {};
    tableData = []
    pageType: any;
    pageSize = '20';
    pageTotal = 0;
    pageIndex = 1;
    loading = false;
    mode = 'query';
    fontSize = '14px';
    lockHeadScroll = { y: window.innerHeight - 350 + 'px'};

    @Input() set params(param) {
        if (param.mode) {
            this.mode = param.mode;
        }
        if (param.pId) {
            this.queryParam.pId = param.pId;
            this.query();
        }
    };
    @Input() set sizeMode(v) {
        this.fontSize = v === 'max' ? '20px' : '14px';
    }

    constructor(
        private http: _HttpClient,
        public statusFollowService: StatusFollowService,
        public message: NzMessageService,
        private datePipe: DatePipe,
        private exportFile: ExportFileService,
        private modalService: NzModalService,
    ) { }

    ngOnInit(): void { 
    }

    formatHref(data) {
        return environment.problem + data.fileUrl;
    }

    query(fromBeginning = false) {
        const param = {
            page: fromBeginning === false ? this.pageIndex : 1,
            limit: this.pageSize,
        };
        Object.assign(param, this.queryParam);
        // this.removePrivateFields(param);
        this.loading = true;
        this.query_detail(param).subscribe(res => {
            this.loading = false;
            res.data.forEach(row => {
                if (row.sysDate) {
                    row._sysDate = new Date(row.sysDate);
                }
            });
            this.tableData = res.data;
            this.pageTotal = res.count;
        });
    }

    deleteRow(row) {
        if (!row.id) {
            return;
        }
        this.modalService.confirm({
            nzTitle: `<i>确认删除</i>`,
            nzWidth: '600',
            nzContent: '确认删除吗?',
            nzOnOk: () => {
                this.delete(row.id).subscribe(res => {
                    if (res.msg === 'Ok') {
                        this.message.success('删除成功');
                        this.query();
                    } else {
                        this.message.warning('删除失败');
                    }
                })
            }
        })
    }

    update(row) {
        const param: any = {};
        Object.assign(param, row);
        this.edit(param).subscribe(res => {
            if (res.msg === 'Ok') {
                this.message.success('保存成功');
                this.query();
            } else {
                this.message.warning('保存失败');
            }
        })
    }

    downloadFile(row) {
        this.exportFile.getDown(environment.problem + '/keyTaskProgress/download?fileUrl='+row.fileUrl +'&fileName='+row.fileName, row.fileName);
    }

    lrsjChange(v, row) {
        row.sysDate = this.datePipe.transform(v, 'yyyy-MM-dd HH:mm');
    }


    /**
     * 完成情况查询
     * @param pId 1
     */
    query_detail(param) {
        const dataurl = environment.problem + '/keyTaskProgress/query';
        return this.http.get(dataurl, param);
    }
 

    /**
     * 修改
     * @param id 1
     * @param lastWeek 上周情况
     * @param totalComplete 累积情况
     * @param thisWeek 这周情况
     * @param difficulty 存在困难
     * @param sysDate 1
     */
    edit(param) {
        const dataurl = environment.problem + '/keyTaskProgress/update';
        return this.http.post(dataurl, param);
    }
    /**
     * 删除
     * @param id 1
     */
    delete(id) {
        if (!id) {
            return;
        }
        const dataurl = environment.problem + '/keyTaskProgress/delete/' + id;
        return this.http.get(dataurl);
    }
}
